package com.stx.mapper;

import com.stx.bean.Zuhu;
import com.stx.utils.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*租户管理读取数据库*/
public class ZuhuMapper {
    //获取租户列表
    public static List<Zuhu> findZuhusList() throws SQLException {
        List<Zuhu> zuhuslist = new ArrayList<>();
        Connection connection = JDBCUtil.getConnection();
        String sql = "select * from zuhu";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String id = resultSet.getString("id");
            String name = resultSet.getString("name");
            String phone = resultSet.getString("phone");
            String house = resultSet.getString("house");
            String dieTime = resultSet.getString("dieTime");
            Zuhu zuhu = new Zuhu(id, name, phone, house, dieTime);
            zuhuslist.add(zuhu);
        }
        return zuhuslist;
    }

    /*添加租户*/
    public static Boolean zuhuAdd(Zuhu zuhu) throws SQLException {
        Connection connection = JDBCUtil.getConnection();
        String sql = "insert into zuhu(id,name,phone,house,dieTime) value (?,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, zuhu.getId());
        preparedStatement.setString(2, zuhu.getName());
        preparedStatement.setString(3, zuhu.getPhone());
        preparedStatement.setString(4, zuhu.getHouse());
        preparedStatement.setString(5, zuhu.getDieTime());
        int i = preparedStatement.executeUpdate();
        return true;
    }

    //根据id查找租户
    public static Zuhu findZuhuById(String id) throws SQLException {
        Connection connection = JDBCUtil.getConnection();
        String sql = "select * from zuhu where id = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, id);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            String name = resultSet.getString("name");
            String phone = resultSet.getString("phone");
            String house = resultSet.getString("house");
            String dieTime = resultSet.getString("dieTime");
            Zuhu zuhu = new Zuhu(id, name, phone, house, dieTime);
            return zuhu;
        }
        return null;
    }

    //更新租户信息
    public static Boolean zuhuUpdate(Zuhu zuhu) throws SQLException {
        Connection connection = JDBCUtil.getConnection();
        String sql = "update zuhu set name=?,phone=?,house=?,dieTime=? where id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, zuhu.getName());
        preparedStatement.setString(2, zuhu.getPhone());
        preparedStatement.setString(3, zuhu.getHouse());
        preparedStatement.setString(4, zuhu.getDieTime());
        preparedStatement.setString(5, zuhu.getId());
        int i = preparedStatement.executeUpdate();
        return true;
    }

    //根据id删除租户信息
    public static Boolean delZuhuById(String id) throws Exception {
        Connection connection = JDBCUtil.getConnection();
        String sql = "delete from zuhu where id =?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, id);
        int i = preparedStatement.executeUpdate();
        return true;
    }
}
